﻿using RimWorld;
using RimWorld.Planet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.AI;
using Verse.AI.Group;

namespace Miao.Stand
{
    public class JobDriver_StandGhostFollow : JobDriver
    {
        StandPawn standPawn
        {
            get
            {
                return pawn as StandPawn;
            }
        }

        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            return true;
        }

        protected override IEnumerable<Toil> MakeNewToils()//感觉游戏本体也是得Toil执行完才可以
        {
            if (standPawn.IsCarryingPawn())//如果携带单位，则走着进入背后灵模式
            {
                IntVec3 ghostPos = standPawn.GetStandGhostPos();
                if (standPawn.Position == ghostPos)
                {
                    yield return GhostFollow();
                }
                else
                {
                    Toil toil = Toils_Goto.GotoCell(standPawn.GetStandGhostPos(), PathEndMode.OnCell);
                    //toil.tickAction += EndJob;
                    toil.FailOn(() => !standPawn.ghostFollow);
                    yield return toil;
                }
            }
            else
            {
                yield return GhostFollow();
            }
            ///这段抄的Goto指令，大篷车离开实际上是Toil来调用的
            ///这段不可能有用，因为根本进不去
            //Toil toil2 = new Toil();
            //toil2.initAction = delegate
            //{
            //    if (pawn.mindState != null && pawn.mindState.forcedGotoPosition == base.TargetA.Cell)
            //    {
            //        pawn.mindState.forcedGotoPosition = IntVec3.Invalid;
            //    }
            //    if (!job.ritualTag.NullOrEmpty())
            //    {
            //        (pawn.GetLord()?.LordJob as LordJob_Ritual)?.AddTagForPawn(pawn, job.ritualTag);
            //    }
            //    if (job.exitMapOnArrival && (pawn.Position.OnEdge(pawn.Map) || pawn.Map.exitMapGrid.IsExitCell(pawn.Position)))//到达时的出口地图
            //    {
            //        TryExitMap();
            //    }
            //};
            //toil2.defaultCompleteMode = ToilCompleteMode.Instant;
            //yield return toil2;
        }
        private Toil GhostFollow()
        {
            Toil toil = new Toil();
            toil.tickAction += this.standPawn.SetStandGhostPos;
            //toil.tickAction = this.standPawn.SetStandGhostPathPos;
            ////toil.tickAction += EndJob;
            toil.defaultCompleteMode = ToilCompleteMode.Never;
            toil.FailOn(() => !standPawn.ghostFollow);//这个就可以阻止背后灵调用状态
            return toil;
        }


        private void TryExitMap()
        {
            if (!job.failIfCantJoinOrCreateCaravan || CaravanExitMapUtility.CanExitMapAndJoinOrCreateCaravanNow(pawn))
            {
                pawn.ExitMap(allowedToJoinOrCreateCaravan: true, CellRect.WholeMap(base.Map).GetClosestEdge(pawn.Position));
            }
        }


        //protected void EndJob()
        //{
        //    if (!standPawn.ghostFollow)
        //    {
        //        this.job.expiryInterval = 1;
        //    }
        //}




    }
}
